<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 
 */
class User_model extends MY_Model
{
	private $table = 'user';
	public function result()
	{
		$where = [];
		if (! in_array($this->session->userdata('user')['id'], config_item('allow_id'))) {
			$where = 'user.id NOT IN('. implode(',', config_item('allow_id')) .')';
		}
		return $this->db
			->select('user.*, role.name AS role_name')
			->join('role', 'role.id = user.role_id', 'LEFT')
			->order_by('user.sort ASC, user.id ASC')
			->get_where($this->table, $where)
			->result();
	}
	/**
	 * [get_one description]
	 * @param  [array]  $where  [description]
	 * @param  [array]  $select [description]
	 * @return [array]          [description]
	 */
	public function row($where = [], $select = [], $join = '')
	{
		if (empty($where)) {
			$this->error = '请输入查询条件';
			return [];
		}

		empty($join) OR $this->db->select('user.*, role.auth')->join('role', 'role.id = user.role_id', 'LEFT');
		return $this->db->select($select)->get_where($this->table, $where)->row();
	}
	/**
	 * [update description]
	 * @param  array  $where [description]
	 * @param  array  $data  [description]
	 * @return [int]         [description]
	 */
	public function update($where = [], $data = [])
	{
		if (empty($where)) {
			$this->error = '请输入条件';
			return 0;
		}

		empty($data) AND $data = $this->get_data($this->table);

		if (empty($data)) {
			$this->error = '没有更新的数据';
			return 0;
		}

		$this->error = '';

		$this->db->update($this->table, $data, $where);
		$affected_rows = $this->db->affected_rows();
		return $affected_rows;
	}
}
